﻿/// This is my first public project :-)
/// feel free to give me some hints

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace BitcoinchartsViewer
{
    public partial class MainForm : Form
    {
        private DataTable _dt = new DataTable();
        private int _timerMinute = 60000;

        public MainForm()
        {
            InitializeComponent();
        }
        public DataTable DT
        {
            get { return _dt; }
            set { _dt = value; }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            String trades = String.Empty;
            DT.Clear();
            try
            {
                trades = Classes.Utils.GetCSV(textBox1.Text, dateTimePicker1.Value, dateTimePicker2.Value);
                DT = Classes.Utils.GetDataTable(trades, ',', DT);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                trades = null;
            }
            if (DT.Rows.Count == 0) DT.Rows.Add(DateTime.Now, 0, 0);
            dataGridView1.DataSource = DT;
            chart1.DataSource = DT;
            FormatChart();
            chart1.DataBind();
            timer1.Interval = (int)numericUpDown1.Value * _timerMinute;
            timer1.Enabled = true;
            label5.ForeColor = Color.Green;
            label5.Text = "running";
        }
        private void FormatChart()
        {
            chart1.ChartAreas[0].AxisX.Interval = 1;
            chart1.ChartAreas[1].AxisX.Interval = 1;
            chart1.ChartAreas[0].AxisY.IsStartedFromZero = false;
            chart1.ChartAreas[0].AxisY.Crossing = (Double)(DT.Compute("min(Preis)", String.Empty));
            chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series[0].XValueMember = "Datum";
            chart1.Series[0].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
            chart1.Series[0].YValueMembers = "Preis";
            chart1.Series[0].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            chart1.Series[0].YAxisType = AxisType.Primary;
            chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
            chart1.Series[1].XValueMember = "Datum";
            chart1.Series[1].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
            chart1.Series[1].YValueMembers = "Menge";
            chart1.Series[1].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            chart1.Series[1].YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Primary;
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            dateTimePicker1.CustomFormat = "dd.MM.yyyy - HH:mm:ss";
            dateTimePicker2.CustomFormat = "dd.MM.yyyy - HH:mm:ss";
            dateTimePicker1.Value = Convert.ToDateTime(DateTime.Now.Date.ToShortDateString() + " " + DateTime.Now.AddHours(-1.0).Hour + ":00:00");
            dateTimePicker2.Value = DateTime.Now;
        }
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            timer1.Interval = (int)numericUpDown1.Value * _timerMinute;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            String trades = String.Empty;
            DateTime von;
            von = Convert.ToDateTime((DT.Compute("max(Datum)", string.Empty)));
            dateTimePicker1.Value = von.AddMinutes(1);
            dateTimePicker2.Value = DateTime.Now;
            trades = Classes.Utils.GetCSV(textBox1.Text, dateTimePicker1.Value, dateTimePicker2.Value);
            DT = Classes.Utils.GetDataTable(trades, ',', DT);
            trades = null;
            dataGridView1.Update();
            chart1.ChartAreas[0].AxisY.Crossing = (Double)(DT.Compute("min(Preis)", String.Empty));
            chart1.DataBind();
        }
    }
}
